package com.example.administrator.bluetoothdemo01;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lzz on 8/5/2017.
 */


/*蓝牙适配器*/
public class BluetoothController {

    private BluetoothAdapter mAdapter;

    public BluetoothController() {
        mAdapter = BluetoothAdapter.getDefaultAdapter();
    }

    public BluetoothAdapter getAdapter() {
        return mAdapter;
    }

    /**
     * 是否支持蓝牙
     */
    public boolean isSupportBlueTooth() {
        if (mAdapter != null) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 判断蓝牙当前状态
     */
    public boolean getBlueToothStatues() {
        assert (mAdapter != null);
        return mAdapter.isEnabled();
    }


    /*打开蓝牙*/
    public void turnOnBluetooth(Activity activity, int requestCode) {
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        activity.startActivityForResult(intent, requestCode);
        /*
        * mAdapter.enable() 不建议
        * */
    }

    /*关闭蓝牙*/
    public boolean turnOffBluetooth() {
        boolean disable = mAdapter.disable();
        return disable;
    }


    /*打开蓝牙可见性*/
    public void enableVisibly(Context context) {
        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); // 设置300秒可搜索
        context.startActivity(discoverableIntent);
    }


    /*
    *
    * 查找设备
    * */
    public void findDevice() {
        assert (mAdapter != null);
        mAdapter.startDiscovery(); // 查找蓝牙 与广播连用
    }


    /*
    *
    * 获得绑定的设备集合
    * */
    public List<BluetoothDevice> getBondeDeviceList(){
        return new ArrayList<>(mAdapter.getBondedDevices());
    }



}
